<!---

Copyright 2008:

Author: Marco van den Oever
Company: WSO
Website: http://www.webserviceonline.biz
Email: marco@webserviceonline.biz

License: 

This software is licensed under the terms of the GNU General Public License version 3 as published by the Free Software Foundation.
For a copy of the GNU General Public License check the license folder or see <http://www.gnu.org/licenses/>.

Description:

wsoFarcryPolling, a Farcry plugin to show pollings on a Farcry based project

Build with great support from the Farcry-Dev group! (Google on Farcry-Dev)

Created: 18/10/08

Tested on CF8

--->
<cfsetting enablecfoutputonly="true" />
<cfimport taglib="/farcry/core/tags/formtools" prefix="ft" />
<cfimport taglib="/farcry/core/tags/admin/" prefix="admin">
<!--- set up page header --->
<admin:header title="Polling Statistics" />
<!--- check if the polling rule is deployed, else show link to deploy --->

<cftry>
  <cfset oFormtoolUtil = createObject("component",
"farcry.core.packages.farcry.formtools") />
  <cfset sqlColumns="objectid" />
  <cfset pollingrule = oFormtoolUtil.getRecordset(sqlColumns=sqlColumns,
sqlWhere="objectid = objectid", typename="rulePolling",
bCheckVersions=true) />
  <cfcatch type="database">
    <cfoutput>The 'Polling' rule has not been deployed yet. Click <a href="/webtop/admin/coapiRules.cfm?deploy=rulePolling">here</a> to deploy it now. </cfoutput>
    <cfabort>
  </cfcatch>
</cftry>
<!--- check if the polling type is deployed, else show link to deploy --->
<cftry>
  <cfset oFormtoolUtil = createObject("component",
"farcry.core.packages.farcry.formtools") />
  <cfset sqlColumns="objectid" />
  <cfset polling = oFormtoolUtil.getRecordset(sqlColumns=sqlColumns,
sqlWhere="objectid = objectid", typename="polling",
bCheckVersions=true) />
  <cfcatch type="database">
    <cfoutput>The 'Polling' content type has not been deployed yet. Click <a href="/webtop/admin/customadmin.cfm?module=customlists/polling.cfm&amp;plugin=wsoFarcryPolling&amp;deploy=true">here</a> to deploy it now.</cfoutput>
    <cfabort>
  </cfcatch>
</cftry>
<!--- check if any polling data exists, else show link to create polling --->
<cfif polling.q.recordcount LE 0>
  <cfoutput>Currently there is no polling to get data from. Click <a href="/webtop/admin/customadmin.cfm?module=customlists/polling.cfm&amp;plugin=wsoFarcryPolling">here</a> to create one now.</cfoutput>
  <cfabort>
</cfif>
<!--- check if the pollingvotes type is deployed, else show link to deploy --->
<cftry>
  <cfset oFormtoolUtil = createObject("component",
"farcry.core.packages.farcry.formtools") />
  <cfset sqlColumns="objectid" />
  <cfset pollingvotes = oFormtoolUtil.getRecordset(sqlColumns=sqlColumns,
sqlWhere="objectid = objectid", typename="pollingvotes",
bCheckVersions=true) />
  <cfcatch type="database">
    <cfoutput>The 'Pollingvotes' content type has not been deployed yet. Click <a href="/webtop/admin/customadmin.cfm?module=customlists/pollingvotes.cfm&amp;plugin=wsoFarcryPolling&amp;deploy=true">here</a> to deploy it now.</cfoutput>
    <cfabort>
  </cfcatch>
</cftry>
<!--- check if any pollingvotes data exists, else show statement --->
<cfif pollingvotes.q.recordcount LE 0>
  <cfoutput>Currently there are no polling votes made.</cfoutput>
  <cfabort>
</cfif>
<!--- get the pollingdata for the drop down menu --->
<cfset obj = createObject('component',application.stcoapi.polling.typePath) />
<cfset stprops_dropdown = {} />
<cfset stprops_dropdown.conditions = {} />
<cfset dropdown = obj.getMultipleByQuery(argumentCollection=stprops_dropdown) />
<!--- if not exists set application.wsoFarcryPolling_stats_objectid --->
<cfif not isdefined('application.wsoFarcryPolling_stats_objectid')>
  <cfset application.wsoFarcryPolling_stats_objectid = dropdown.objectid />
</cfif>
<!--- get the pollingdata based on current application.wsoFarcryPolling_stats_objectid --->
<cfset obj = createObject('component',application.stcoapi.polling.typePath) />
<cfset stprops_pollingdata = {} />
<cfset stprops_pollingdata = obj.getData(application.wsoFarcryPolling_stats_objectid) />
<!--- set stprops_pollingdata.objectid in valid variable name for use as object identifier --->
<cfset pollingid = replace(stprops_pollingdata.objectid,'-','','ALL') />
<!--- set application['wsoFarcryPolling' & pollingid] to set db question count --->
<cfset application['wsoFarcryPolling' & pollingid] = listLen(stprops_pollingdata.answers) />
<cfoutput>
  <h1>Polling Statistics</h1>
  <!--- if form was submitted check for action --->
  <cfif isdefined('form.polling')>
    <!--- if the form was submitted by the select button, select correct data --->
    <ft:processform action="Select">
      <!--- get the pollingdata based on current application.wsoFarcryPolling_stats_objectid --->
      <cfset obj = createObject('component',application.stcoapi.polling.typePath) />
      <cfset stprops_pollingdata = {} />
      <cfset stprops_pollingdata = obj.getData(form.polling) />
      <!--- set application.wsoFarcryPolling_stats_objectid value to pollingdata.objectid value --->
      <cfset application.wsoFarcryPolling_stats_objectid = "#stprops_pollingdata.objectid#">
    </ft:processform>
    <!--- if the form was submitted by the edit button, redirect to correct edit page --->
    <ft:processform action="Edit">
      <cflocation url="/webtop/conjuror/invocation.cfm?objectid=#application.wsoFarcryPolling_stats_objectid#&amp;typename=polling&amp;method=edit&amp;ref=typeadmin&amp;module=customlists/polling.cfm&amp;plugin=wsoFarcryPolling" addtoken="no">
    </ft:processform>
  </cfif>
  <!--- get the polling data stats --->
  <!--- set default answer_sum so calculate can be done --->
  <cfset answers_sum = 0>
  <cfloop index="i" from="1" to="#application['wsoFarcryPolling' & pollingid]#">
    <cfset oFormtoolUtil = createObject("component",
"farcry.core.packages.farcry.formtools") />
    <cfset sqlColumns="objectid" />
    <cfset answer = oFormtoolUtil.getRecordset(sqlColumns=sqlColumns,
sqlWhere="answer = '#i#' AND pollingobjectid = '#application.wsoFarcryPolling_stats_objectid#'", typename="pollingvotes",
bCheckVersions=true) />
    <cfset VARIABLES['answersum' & i]  = answer.q.recordcount>
    <!--- get the total votes of all answers that exists --->
    <cfset answers_sum = answers_sum + #VARIABLES['answersum' & i]#>
  </cfloop>
  <!--- show the polling results --->
  <table>
    <ft:form>
      <tr>
        <td colspan="2" align="left">&nbsp;</td>
      </tr>
      <tr>
        <td><select name="polling">
            <cfloop query="dropdown" startrow="1" endrow="#application['wsoFarcryPolling' & pollingid]#">
              <option value="#dropdown.ObjectID#"<cfif #dropdown.ObjectID# EQ #application.wsoFarcryPolling_stats_objectid#> selected="selected"</cfif>>#dropdown.question#</option>
            </cfloop>
          </select></td>
        <td align="left"><ft:button value="Select" size="small" />
          <ft:button value="Edit" size="small" />
          <ft:button value="xls" size="small" />
        </td>
      </tr>
    </ft:form>
    <tr>
      <td colspan="2" align="left">&nbsp;</td>
    </tr>
    <tr>
      <td colspan="2" align="left"><!--- show the polling results in cfchart --->
        <cfchart format="png"
chartwidth="300" chartheight="300" showlegend="yes" show3d="yes">
        <cfchartseries type="pie">
        <cfloop index="i" from="1" to="#application['wsoFarcryPolling' & pollingid]#">
          <cfif listGetAt(stprops_pollingdata.answers, #i#) GTE 0>
            <cfset answer = listGetAt(stprops_pollingdata.answers, #i#) />
            <cfchartdata item="#answer# (#VARIABLES['answersum' & i]#)" value="#VARIABLES['answersum' & i]#">
          </cfif>
        </cfloop>
        <cfchartdata item="Total votes (#answers_sum#)" value="#answers_sum#">
        </cfchartseries>
        </cfchart>
      </td>
    </tr>
  </table>
  <!--- if the form was submitted by the Excel button, save data to Excel --->
  <ft:processform action="xls">
    <!--- Import the POI tag library. --->
    <cfimport taglib="/farcry/plugins/wsoFarcryPolling/packages/custom/poi/" prefix="poi" />
    <!--- 
	Create an excel document and store binary data into 
	REQUEST variable. 
--->
    <poi:document 
	name="REQUEST.ExcelData"
	file="#ExpandPath( './polling_statistics.xls' )#"
	style="font-family: verdana ; font-size: 10pt ; color: black ; white-space: nowrap ;">
      <!--- Define style classes. --->
      <poi:classes>
        <poi:class
			name="title"
			style="font-family: verdana ; font-size: 12pt ; color: black ; text-align: center ;"
			/>
        <poi:class 
			name="header" 
			style="font-family: verdana ; font-size: 10pt ; color: black ; background-color: GREY_25_PERCENT ; border-bottom: solid 3px GREY_40_PERCENT ;" 
			/>
      </poi:classes>
      <!--- Define Sheets. --->
      <poi:sheets>
        <poi:sheet 
			name="polling_statistics"
			freezerow="2"
			orientation="landscape"
			zoom="100%">
          <!--- Define global column styles. --->
          <poi:columns>
            <poi:column style="width: 200px ; text-align: center ;" />
            <poi:column style="width: 150px ;" />
            <cfloop index="i" from="1" to="#application['wsoFarcryPolling' & pollingid]#">
              <cfif listGetAt(stprops_pollingdata.answers, #i#) GTE 0>
                <poi:column style="width: 150px ; text-align: center ;" />
              </cfif>
            </cfloop>
            <poi:column style="width: 200px ; text-align: center ;" />
          </poi:columns>
          <!--- Set title colspan value based on polling answers and to show fields count --->
          <cfloop index="i" from="1" to="#application['wsoFarcryPolling' & pollingid]#">
            <cfif listGetAt(stprops_pollingdata.answers, #i#) GTE 0>
              <cfset colspanval = #i# + 3>
            </cfif>
          </cfloop>
          <!--- Title row. --->
          <poi:row class="title">
            <poi:cell value="polling_statistics totals" colspan="#colspanval#" />
          </poi:row>
          <!--- Header row. --->
          <poi:row class="header">
            <poi:cell value="Date / Time created" />
            <poi:cell value="Question" />
            <cfloop index="i" from="1" to="#application['wsoFarcryPolling' & pollingid]#">
              <cfif listGetAt(stprops_pollingdata.answers, #i#) GTE 0>
                <cfset answer = listGetAt(stprops_pollingdata.answers, i)>
                <poi:cell value="#answer#" />
              </cfif>
            </cfloop>
            <poi:cell value="Total votes" />
          </poi:row>
          <!--- Output the polling data --->
          <poi:row>
            <poi:cell type="date" value="#stprops_pollingdata.datetimecreated#" />
            <poi:cell value="#stprops_pollingdata.question#" />
            <cfloop index="i" from="1" to="#application['wsoFarcryPolling' & pollingid]#">
              <cfif listGetAt(stprops_pollingdata.answers, #i#) GTE 0>
                <poi:cell type="numeric" value="#VARIABLES['answersum' & i]#" />
              </cfif>
            </cfloop>
            <poi:cell type="numeric" value="#answers_sum#" />
          </poi:row>
          <!--- Title row. --->
          <poi:row class="title">
            <poi:cell value="polling_statistics votes" colspan="#colspanval#" />
          </poi:row>
          <!--- Header row. --->
          <poi:row class="header">
            <poi:cell value="Date / Time created" />
            <poi:cell value="Question" />
            <cfloop index="i" from="1" to="#application['wsoFarcryPolling' & pollingid]#">
              <cfif listGetAt(stprops_pollingdata.answers, #i#) GTE 0>
                <cfset answer = listGetAt(stprops_pollingdata.answers, i)>
                <poi:cell value="#answer#" />
              </cfif>
            </cfloop>
            <poi:cell value="IP" />
          </poi:row>
          <!--- get all the pollingvotes to list in Excel --->
          <cfset obj = createObject('component',application.stcoapi.pollingvotes.typePath) />
          <cfset stprops_votes = {} />
          <cfset stprops_votes.conditions = {} />
          <cfset votes = obj.getMultipleByQuery(argumentCollection=stprops_votes) />
          <!--- get the pollingvotes data for current polling --->
          <cfloop query="votes" startrow="1" endrow="#votes.recordcount#">
            <poi:row>
              <poi:cell type="date" value="#votes.datetimecreated#" />
              <poi:cell value="#stprops_pollingdata.question#" />
              <cfloop index="i" from="1" to="#application['wsoFarcryPolling' & pollingid]#">
                <cfif #votes.answer[currentrow]# EQ #i#>
                  <cfset VARIABLES['ipanswer' & i] = 1>
                  <cfelse>
                  <cfset VARIABLES['ipanswer' & i] = 0>
                </cfif>
                <poi:cell type="numeric" value="#VARIABLES['ipanswer' & i]#" />
              </cfloop>
              <poi:cell value="#votes.userip#" />
            </poi:row>
          </cfloop>
        </poi:sheet>
      </poi:sheets>
    </poi:document>
    <!--- Tell the browser to expect an Excel file attachment. --->
    <cfheader
	name="content-disposition"
	value="attachment; filename=polling_statistics.xls"
	/>
    <!--- 
	Tell browser the length of the byte array output stream. 
	This will help the browser provide download duration to 
	the user. 
--->
    <cfheader
	name="content-length"
	value="#REQUEST.ExcelData.Size()#"
	/>
    <!--- Stream the binary data to the user. --->
    <cfcontent
	type="application/excel"
	variable="#REQUEST.ExcelData.ToByteArray()#"
	/>
  </ft:processform>
</cfoutput>
<!--- setup footer --->
<admin:footer>
<cfsetting enablecfoutputonly="false" />
